const app = getApp()
const db=wx.cloud.database()
const produce_db=db.collection('produce')
Page({
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    Custom: app.globalData.Custom,
    TabCur: 0,
    MainCur: 0,
    VerticalNavTop: 0,
    keyword:[],
    list: [
      {
        'name':'戏服','id':0,'items':[]
      },{
        'name':'道具','id':1,'items':[]
      },{
        'name':'乐器','id':2,'items':[]
      },{
        'name':'戏单','id':3,'items':[]
      }
      ,{
        'name':'杂项','id':4,'items':[]
      }
    ],
    load: true,
    // items:['戏服','道具','乐器','戏单','杂项']
  },
  searchValue(e){
    let val = e.detail.value;
    this.setData({ value:val })
    if(val.length > 0){
      this.setData({ filterList:[] })
      let arr = [];
      for(let i = 0;i < this.data.keyword.length;i++){
          if(this.data.keyword[i].name.indexOf(val) > -1){
            if(!arr.includes(this.data.keyword[i].name)){
              arr.push(this.data.keyword[i].name)
              console.log(arr)
            }
            
          }
      }

      this.setData({ filterList: arr })
      if(this.data.filterList.length==0){
        this.setData({
          filterList:['未找到相关内容']
        })
      }
    }else{
      this.setData({ filterList: [] })
    }
  },
  // 完全匹配搜索
  
  // 选中
  pitchOn(e){
    if(this.data.filterList[0]!='未找到相关内容'){
    let index = e.currentTarget.dataset.index;
    // wx.showToast({ title: `选中：${this.data.filterList[index]}`, icon: "none" });
    console.log(this.data.filterList[index])
    for(var i = 0 ; i< this.data.keyword.length; i ++){
      if(this.data.filterList[index]==this.data.keyword[i].name){
        this.setData({
          t:[],
          filterList:[]
        })
        wx.setStorageSync('detail', this.data.keyword[i])
        wx.navigateTo({
          url: '/pages/home/produce/detail'
        })
      }
    }
    
  }
  },
  tips(){
    for(var i = 0; i<this.data.list.length; i++){
      for(var a =0; a<this.data.list[i].items.length;a++){
        this.data.keyword.push(this.data.list[i].items[a])
        // console.log(this.data.list[i].items[a])
      }
    }
    // console.log(this.data.keyword)
  },
  changePage(e){
    let id = e.currentTarget.dataset.id
    wx.setStorageSync('detail', id)
    console.log(id)
    wx.navigateTo({
      url: '/pages/home/produce/detail'
    })
  },
  async onLoad() { 
    var that=this

    wx.showLoading({ 
      title: '加载中...',
      mask: true
    });
    let count = await produce_db.count()
    count=count.total
    let all=[]
    for(let i =0 ; i<count; i+=20){
      let list= await produce_db.skip(i).get()
      all = all.concat(list.data)
    }
    console.log(all)
    // produce_db
    // .get()
    // .then(res=>{
    //   console.log(res.data)
      for(var i = 0;i<all.length;i++){
        if(all[i].kind=='戏服'){
          that.data.list[0].items.push(all[i])
        }else if(all[i].kind=='道具'){
          that.data.list[1].items.push(all[i])
        }else if(all[i].kind=='乐器'){
          that.data.list[2].items.push(all[i])
        }else if(all[i].kind=='戏单'){
          that.data.list[3].items.push(all[i])
        }else if(all[i].kind=='杂项'){
          that.data.list[4].items.push(all[i])
        }
      }
      that.setData({
        list: that.data.list,
        listCur: that.data.list[0]
      })
      that.tips()
  },
  onReady() {
    wx.hideLoading()
  },
  tabSelect(e) {
    this.setData({
      TabCur: e.currentTarget.dataset.id,
      MainCur: e.currentTarget.dataset.id,
      VerticalNavTop: (e.currentTarget.dataset.id - 1) * 50
    })
  },
  VerticalMain(e) {
    let that = this;
    let list = this.data.list;
    let tabHeight = 0;
    if (this.data.load) {
      for (let i = 0; i < list.length; i++) {
        let view = wx.createSelectorQuery().select("#main-" + list[i].id);
        view.fields({
          size: true
        }, data => {
          list[i].top = tabHeight;
          tabHeight = tabHeight + data.height;
          list[i].bottom = tabHeight;     
        }).exec();
      }
      that.setData({
        load: false,
        list: list
      })
    }
    let scrollTop = e.detail.scrollTop ;
    for (let i = 0; i < list.length; i++) {
      if (scrollTop > list[i].top && scrollTop < list[i].bottom) {
        that.setData({
          // VerticalNavTop: (list[i].id - 1) * 50,
          TabCur: list[i].id
        })
        return false
      }
    }
  },
  sc(e){
    var i = e.currentTarget.dataset.content
    console.log(i)
  },
})